package com.seaking.controller;

import com.seaking.util.CommonJson;
import com.seaking.util.VerificationUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created by wchha on 9/23 0023.
 */
@Controller
public class VerificationController {

    /**
     * 验证码验证
     * @param session
     * @param re
     */
    @RequestMapping(value="/verification",method = RequestMethod.GET)
    public void getVerification(HttpSession session, HttpServletResponse re){
        BufferedImage image = VerificationUtils.verification(session);
        try {
            ImageIO.write(image,"jpg",re.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * ajax异步验证
     * @param code
     * @param session
     * @return
     */
    @RequestMapping(value="/verificationCode",method = RequestMethod.GET)
    @ResponseBody
    public CommonJson verificationCode(String code,HttpSession session){
        String verification = (String) session.getAttribute("verification");
        verification = verification.toLowerCase();
        code = code.toLowerCase();
        CommonJson json = new CommonJson();
        if(verification.equals(code)){
            return json;
        }else {
            json.setSuccess(false);
            return json;
        }
    }
}
